package com.mkbk.controller;

import com.google.gson.Gson;
import com.mkbk.model.Component;
import com.mkbk.service.ComponentService;
import com.mkbk.service.impl.ComponentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/component")
public class ComponentServlet extends HttpServlet {
    private final ComponentService componentService = new ComponentServiceImpl();
    private static final Gson gson = new Gson();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, ServletException, IOException {
        super.service(req, res);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        handleRequest(req, res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        handleRequest(req, res);
    }

    private void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {
        req.setCharacterEncoding("utf-8");
        res.setHeader("Access-Control-Allow-Origin", "*");
        String optionType = req.getParameter("optionType");
        res.setContentType("application/json;charset=utf-8");
        String response;
        if ("search".equals(optionType)) {
            response = handleSearch(req);
        } else if ("page".equals(optionType)) {
            response = handlePage(req);
        } else if ("add".equals(optionType)) {
            response = handleAdd(req);
        } else if ("delete".equals(optionType)) {
            response = handleDelete(req);
        } else if ("update".equals(optionType)) {
            response = handleUpdate(req);
        } else if ("detail".equals(optionType)) {
            response = handleGetById(req);
        } else {
            response = "{\"status\":\"Invalid optionType\"}";
        }
        res.getWriter().write(response);
    }

    private String handleSearch(HttpServletRequest req) {
        Component component = new Component();
        component.setcName(req.getParameter("cName"));
        List<Component> componentList = componentService.getAllComponent(component);
        return gson.toJson(componentList);
    }

    private String handlePage(HttpServletRequest req) {
        Component component = new Component();
        int current = Integer.parseInt(req.getParameter("current"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        List<Component> componentList = componentService.getAllComponentByPage(component, current, pageSize);
        return gson.toJson(componentList);
    }

    private String handleAdd(HttpServletRequest req) {
        Component component = new Component();
        component.setcName(req.getParameter("cName"));
        component.setCount(Integer.valueOf(req.getParameter("count")));
        component.setPrice(Double.parseDouble(req.getParameter("price")));
        component.setDescription(req.getParameter("description"));
        component.setStandard(req.getParameter("standard"));
        componentService.addComponent(component);
        return "{\"status\":\"添加成功\"}";
    }

    private String handleDelete(HttpServletRequest req) {
        componentService.deleteComponentById(Integer.parseInt(req.getParameter("id")));
        return "{\"status\":\"删除成功\"}";
    }


    private String handleUpdate(HttpServletRequest req) {
        Component component = new Component();
        component.setId(Integer.parseInt(req.getParameter("id")));
        component.setcName(req.getParameter("cName"));
        component.setCount(Integer.valueOf(req.getParameter("count")));
        component.setPrice(Double.parseDouble(req.getParameter("price")));
        component.setStandard(req.getParameter("standard"));
        component.setDescription(req.getParameter("description"));
        componentService.updateComponentById(component);
        return "{\"status\":\"修改成功\"}";
    }

    private String handleGetById(HttpServletRequest req) {
        Component component = componentService.getComponentById(Integer.parseInt(req.getParameter("id")));
        return gson.toJson(component);
    }
}
